<template>
  <ContentWrap>
    <!--    顶部查询项-->
    <el-form :model="form" inline label-width="100px">
      <el-form-item :label="t('category.name')">
        <el-input
          v-model="form.name"
          clearable
          :placeholder="'请输入' + t('category.name')"
          style="width: 250px"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="initialize">查询</el-button>
        <el-button icon="Refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <!--    操作项-->
    <div class="flex flex-items-center my-5">
      <el-button type="primary" @click="handleAdd('add')">
        {{ t('action.add') }}
      </el-button>
      <el-button type="danger" @click="handleDel(null)">
        {{ t('action.del') }}
      </el-button>
    </div>
    <!-- 表格主体 -->
    <el-table
      ref="elTableExpose"
      :data="dataList"
      highlight-current-row
      stripe
      border
      v-loading="tableLoading"
      :cell-style="{ textAlign: 'center' }"
      :header-cell-style="{ textAlign: 'center' }"
    >
      <el-table-column type="selection" width="55" />

      <el-table-column :label="t('category.id')" prop="id" min-width="70" />

      <el-table-column
        :label="t('category.parent_id')"
        prop="parentName"
        :show-overflow-tooltip="true"
      />

      <el-table-column :label="t('category.name')" prop="name" :show-overflow-tooltip="true" />
      <el-table-column
        :label="t('category.description')"
        prop="description"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="t('category.sort_order')"
        prop="sort_order"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        :label="t('category.solution_type')"
        prop="solution_type"
        :show-overflow-tooltip="true"
      >
        <template #default="{ row }">
          {{ row.solution_type === 1 ? '是' : '否' }}
        </template>
      </el-table-column>

      <el-table-column
        :label="t('category.creator')"
        prop="create_name"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        :label="t('category.create_time')"
        prop="create_time"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="t('category.updater')"
        prop="update_name"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        :label="t('category.update_time')"
        prop="update_time"
        :width="calculateColumnWidth(dataList, 'update_time')"
        :show-overflow-tooltip="true"
      />
      <el-table-column fixed="right" label="操作" width="200">
        <template #default="{ row }">
          <el-button @click="handleJump('edit', row)" size="small">
            {{ t('order.edit') }}
          </el-button>

          <el-button type="success" @click="handleJump('detail', row)" size="small">
            详情
          </el-button>

          <el-button type="danger" @click="handleDel(row)" size="small">
            {{ t('action.del') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <Pagination
      :total="paginationTotal"
      v-model:page="form.pageIndex"
      v-model:limit="form.pageSize"
      @pagination="initialize"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import dayjs from 'dayjs'
import Pagination from '@/components/Pagination/index.vue'
import { ContentWrap } from '@/components/ContentWrap'
import { calculateColumnWidth } from '@/utils'
import { getListApi, delListApi } from '@/api/category'
import { ElMessage } from 'element-plus'

const { t } = useI18n()
const { push } = useRouter()

const tableLoading = ref<boolean>(false)
const dataList = ref<any[]>([])
const paginationTotal = ref<number>(0)

const form = ref<Record<string, any>>({
  name: '',
  pageIndex: 1,
  pageSize: 30
})

const elTableExpose = ref<any>()

//点击跳转到新增
const handleAdd = (type: string) => {
  push(`/product/category-management/${type}`)
}

//删除
const handleDel = async (row) => {
  console.log(row)
  const ids = row ? [row.id] : elTableExpose.value?.getSelectionRows().map((v: any) => v.id) || []
  if (ids.length === 0) {
    ElMessage.error(t('common.selectData'))
    return
  }
  const resp = await delListApi(ids)
  if (resp.code === 0) {
    ElMessage.success(t('common.deleteSucMsg'))
    initialize()
  }
}
//跳转
const handleJump = (type: string, row: Record<string, any>) => {
  console.log(row)
  // return
  const path = ref<string>('') // 默认值为空字符串
  path.value = `/product/category-management/${type}`
  push({ path: path.value, query: { id: row.id } })
}

//初始化请求
const initialize = async () => {
  tableLoading.value = true
  const params = JSON.parse(JSON.stringify(form.value))
  delete params.date
  if (params.order_no && !params.order_type) {
    ElMessage.error('请选择' + t('category.order_type'))
    tableLoading.value = false
    return
  }
  const resp = await getListApi(params)
  dataList.value = resp.data.list

  paginationTotal.value = resp.data.total
  tableLoading.value = false
}

//重置查询项
const reset = () => {
  form.value = {
    name: '',
    pageIndex: 1,
    pageSize: 30
  }
  paginationTotal.value = 0
  initialize()
}

onMounted(() => {
  // getDropDownBoxInfo()
  initialize()
})
</script>

<style lang="less" scoped></style>
